const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const fs = require("fs");

//获取客户端 ip 地址
app.use(function (request, response, next) {
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.setHeader("Access-Control-Allow-Headers", "*");
  response.setHeader("Access-Control-Allow-Methods", "*");
  next();
});

app.all("/movie", (req, res) => {
  setTimeout(() => {
    res.json([
      {
        id: 1,
        name: "让子弹飞",
        director: "美文",
      },
      {
        id: 2,
        name: "阿凡达",
        director: "卡梅隆",
      },
    ]);
  }, 3000);
});

app.all("*", (req, res) => {
  if (req.method.toUpperCase() === "OPTIONS") {
    res.status(200).end("ok");
    return;
  }
  res.status(404);
  res.end("not found");
});

app.listen(8080, () => {
  console.log("服务已经启动....8080");
});
